/*
 * watchdog.c
 *
 *  Created on: 2017年5月12日
 *      Author: chenss
 */


#include <io/watchdog.h>
#include "message_queue.h"

// 看门狗定时时长（秒）
#define WATCH_DOG_INTERVAL 25

static int _value;

void WatchDog_Init(void)
{
    _value = 0;

    /* Enable write access to IWDG_PR and IWDG_RLR registers */
    IWDG_WriteAccessCmd(IWDG_WriteAccess_Enable);

    /* IWDG counter clock: LSI/256 */
    IWDG_SetPrescaler(IWDG_Prescaler_256);

    /* Set counter reload value */
    IWDG_SetReload(WATCH_DOG_INTERVAL * LSI_VALUE / 256);

    /* Reload IWDG counter */
    IWDG_ReloadCounter();

    /* Enable IWDG (the LSI oscillator will be enabled by hardware) */
    IWDG_Enable();
}

int WatchDog_Get(void)
{
    return _value;
}

void WatchDog_Set(int value)
{
    _value = value;
//    debug("WatchDog: %d\r\n", value);
}

void WatchDog_OnReset(void)
{
    /* Reload IWDG counter */
    IWDG_ReloadCounter();
}

void WatchDog_Feed(void)
{
    MsgQueue_Send(MsgQueue_Id_WatchDogReset, NULL, 0);
}


